<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // function MyNew(func, ...args) {
        //     if (typeof func !== 'function') {
        //         throw '第一个参数必须是函数';
        //     }

        //     const obj = {};
        //     obj.__proto__ = Object.create(func.prototype);

        //     let result = func.call(obj, args);

        //     let isObj = typeof result == 'object' && result !== null;
        //     let isFun = typeof result == 'function';

        //     return isObj || isFun ? result : obj;
        // }

        function MyNew(fn,...args) {  
            if(typeof fn !== 'function'){
                throw '第一个参数必须是函数'
            }

            let obj = {};
            obj.__proto__ = Object.create(fn.prototype)

            let result = fn.apply(obj,args)

            let isObj = typeof result == 'object' && result !== null;
            let isFun = typeof result == 'function';

            return isObj || isFun ? result : obj;
        }

        function Person(name) {
            this.name = name;
        }

        Person.prototype.call = function () {
            console.log(this.name);
        }

        let person = MyNew(Person, 'xiaoming');

        console.log(person);
        person.call();
        console.log(typeof Object);
    </script>
</body>

</html>